package cs.oor;

import java.net.MalformedURLException;
import java.net.URL;

import android.app.TabActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.Resources;

public class TabLayout extends TabActivity {

    protected int refreshMenuId;

    protected OrderRetrievalThread thread;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();

        TabHost.TabSpec spec;

        Intent intent;

        intent = new Intent().setClass(this, ConfigActivity.class);
        spec = tabHost.newTabSpec("config").setIndicator("Config").setContent(intent);

        tabHost.addTab(spec);

        intent = new Intent().setClass(this, OrderListActivity.class);
        spec = tabHost.newTabSpec("list").setIndicator("Orders").setContent(intent);

        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
/*
        Intent serviceIntent = new Intent(getApplicationContext(), OrderRetrievalService.class);

        SharedPreferences settings = getSharedPreferences(ConfigActivity.PREFS_NAME, 0);

        
        serviceIntent.setData(Uri.parse(settings.getString(Config.SHOP_URL, "")));

        startService(serviceIntent);
        
        */
        
        thread = new OrderRetrievalThread(getApplicationContext());
        thread.setDaemon(true);
        thread.start();
    }
    
    
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        Intent intent = new Intent(getApplicationContext(), OrderRetrievalService.class);
        SharedPreferences settings = getSharedPreferences(Config.PREFS_NAME, 0);
        
        intent.setData(Uri.parse(settings.getString(Config.SHOP_URL, "")));
        
        MenuItem refresh = menu.findItem(R.id.refresh_option_item);
        refresh.setIntent(intent);
        
        refreshMenuId = refresh.getItemId(); 
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == refreshMenuId) {
            startService(item.getIntent());
        }
        
        return true;
    }
}
